<template>
  <el-dialog v-model="dialogVisible" title="导入逻辑选择" width="600px" :before-close="handleClose" class="import-dialog">
    <div class="import-content">
      <!-- 下载模板 -->
      <div class="template-section">
        <div class="template-info">
          <el-icon class="info-icon"><InfoFilled /></el-icon>
          <span>请先下载模板，按照模板格式填写数据后再导入</span>
        </div>
        <el-button type="primary" @click="handleDownloadTemplate">
          <el-icon><Download /></el-icon>
          下载模板
        </el-button>
      </div>

      <!-- 导入逻辑选择 -->
      <div class="option-cards">
        <div :class="['option-card', { active: selectedOption === 1 }]" @click="selectedOption = 1">
          <div class="option-header">导入逻辑1</div>
          <div class="option-description">导入参数有存有序水时，默认导入失败，不执行实验。</div>
          <div class="option-checkmark" v-if="selectedOption === 1">
            <el-icon><Check /></el-icon>
          </div>
        </div>

        <div :class="['option-card', { active: selectedOption === 2 }]" @click="selectedOption = 2">
          <div class="option-header">导入逻辑2</div>
          <div class="option-description">导入参数有存有序水时，只跳该原液单独新建内部配方参数。</div>
          <div class="option-checkmark" v-if="selectedOption === 2">
            <el-icon><Check /></el-icon>
          </div>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, watch } from "vue"
import { Check, Download, InfoFilled } from "@element-plus/icons-vue"
import { ElMessage } from "element-plus"

interface Props {
  modelValue: boolean
}

interface Emits {
  (e: "update:modelValue", value: boolean): void
  (e: "confirm", option: number): void
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const dialogVisible = ref(false)
const selectedOption = ref<number>(1)

// 下载模板
const handleDownloadTemplate = () => {
  // TODO: 实现模板下载逻辑
  ElMessage.success("模板下载成功")
  // 例如：window.open('/api/download/template.xlsx')
}

watch(
  () => props.modelValue,
  val => {
    dialogVisible.value = val
  }
)

watch(dialogVisible, val => {
  emit("update:modelValue", val)
})

const handleClose = () => {
  dialogVisible.value = false
  selectedOption.value = 1
}

const handleConfirm = () => {
  emit("confirm", selectedOption.value)
  handleClose()
}
</script>

<style lang="scss" scoped>
.import-dialog {
  :deep(.el-dialog__header) {
    padding: 20px 24px;
    margin-right: 0;
    border-bottom: 1px solid #f0f0f0;
    .el-dialog__title {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
    }
  }
  :deep(.el-dialog__body) {
    padding: 32px 24px;
  }
  :deep(.el-dialog__footer) {
    padding: 16px 24px;
    border-top: 1px solid #f0f0f0;
  }
}
.import-content {
  .template-section {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    margin-bottom: 24px;
    background: #f0f9ff;
    border: 1px solid #bfdbfe;
    border-radius: 8px;
    .template-info {
      display: flex;
      gap: 8px;
      align-items: center;
      font-size: 14px;
      color: #1e40af;
      .info-icon {
        font-size: 18px;
        color: #3b82f6;
      }
    }
    .el-button {
      flex-shrink: 0;
    }
  }
  .option-cards {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;
    .option-card {
      position: relative;
      display: flex;
      flex-direction: column;
      min-height: 160px;
      padding: 24px 20px;
      cursor: pointer;
      background: #ffffff;
      border: 2px solid #e4e7ed;
      border-radius: 8px;
      transition: all 0.3s;
      &:hover {
        border-color: var(--el-color-primary);
        box-shadow: 0 2px 12px 0 rgb(64 158 255 / 15%);
      }
      &.active {
        background: linear-gradient(135deg, rgb(64 158 255 / 5%) 0%, rgb(64 158 255 / 2%) 100%);
        border-color: var(--el-color-primary);
        box-shadow: 0 2px 12px 0 rgb(64 158 255 / 20%);
        .option-header {
          color: var(--el-color-primary);
        }
      }
      .option-header {
        margin-bottom: 16px;
        font-size: 16px;
        font-weight: 600;
        color: #303133;
        text-align: center;
      }
      .option-description {
        display: flex;
        flex: 1;
        align-items: center;
        font-size: 14px;
        line-height: 1.6;
        color: #606266;
        text-align: left;
      }
      .option-checkmark {
        position: absolute;
        top: 12px;
        right: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        width: 24px;
        height: 24px;
        font-size: 14px;
        color: #ffffff;
        background: var(--el-color-primary);
        border-radius: 50%;
        animation: scale-in 0.3s ease;

        @keyframes scale-in {
          from {
            transform: scale(0);
          }
          to {
            transform: scale(1);
          }
        }
      }
    }
  }
}
.dialog-footer {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  .el-button {
    min-width: 80px;
  }
}
</style>
